#region License
// Copyright (c) 2012 Mund Consulting Ltd
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
using System.Globalization;
using System.IO;
using Microsoft.SharePoint.Utilities;

namespace Mund.Sharepoint.Publishing.Features.Feature2
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("206d4727-9e89-426e-b5a5-a96e50710787")]
    public class Feature2EventReceiver : SPFeatureReceiver
    {
        const string constResource = "MundPublishingResource";
        const string constListPageLayoutTitle = "pageLayoutTitle";
        const string constListPagesTitle = "pages";
        const string constListMundStyleLibrary = "mundStyleLibrary";
        const string constMundMasterPagePath = "/_catalogs/masterpage/mund.master";
        const string constmundMasterPageFilesPath = "Style Library/mc";
        const string constDefaultPage = "Pages/default.aspx";
        const string constException = "When Microsoft Sharepoint Publishing Infrastructure activated, Mund Publishing Infrastructure can't be activated.";
        const string constSharepointPublishingFId = "f6924d36-2fa8-4f0b-b16d-06b7250180fa";
        const string constPropertyForOldCustomMaster = "Mund_OldCustomMaster";
        const string constPropertyForOldWelcomePage = "Mund_OldWelcoMePage";
        const string constServerRelativeRootWeb = "/";
        const string constResourcePathFormat = "{0}\\Resources\\";
        const string constAppGlobalResource="App_GlobalResources";
        const string constResourceFileName = "MundPublishingResource.resx";

        /// <summary>
        /// 1.If Microsoft Sharepoint Publishing Infrastructure is enabled,This feature won't be activated.
        /// 2.CustomMasterPage changed to mund.master.
        /// 3.Site Welcome page changed to Pages/default.aspx.
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            SPWeb curWeb = (SPWeb)properties.Feature.Parent;
            curWeb.AllProperties[constPropertyForOldCustomMaster] = curWeb.CustomMasterUrl;

            //Checks whether the site collection is on default path or on managed path
            if (curWeb.ServerRelativeUrl != constServerRelativeRootWeb)
                curWeb.CustomMasterUrl = curWeb.ServerRelativeUrl + constMundMasterPagePath;
            else
                curWeb.CustomMasterUrl = constMundMasterPagePath;

            SPFolder rootFolder = curWeb.RootFolder;
            curWeb.AllProperties[constPropertyForOldWelcomePage] = rootFolder.WelcomePage;
            rootFolder.WelcomePage = constDefaultPage;
            rootFolder.Update();
            curWeb.Update();         
        }
        /// <summary>
        /// 1.PageLayouts library Deleted.
        /// 2.Pages library deleted.
        /// 3.MundStyleLibrary deleted
        /// 4.CustomMasterPage changed to v4.master.
        /// 5.Site welcome page restored to the sitepages/home.aspx
        /// 6.Web Properties deleted.
        /// 7.Mund.Master file deleted from masterpage catalog.
        /// 8.MasterPage Resorces folder 'Style Library/mc' deleted from style library.
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb curWeb = (SPWeb)properties.Feature.Parent;
            SPList pageLayouts = curWeb.Lists.TryGetList(Localization.GetValue(constResource, constListPageLayoutTitle));
            if (pageLayouts != null)
            {
                pageLayouts.Delete();
            }

            SPList pages = curWeb.Lists.TryGetList(Localization.GetValue(constResource, constListPagesTitle));
            if (pages != null)
            {
                pages.Delete();
            }

            SPList MundStyleLibrary = curWeb.Lists.TryGetList(Localization.GetValue(constResource, constListMundStyleLibrary));
            if (MundStyleLibrary != null)
            {
                MundStyleLibrary.Delete();
            }

            curWeb.CustomMasterUrl = curWeb.AllProperties[constPropertyForOldCustomMaster].ToString();

            SPFolder rootFolder = curWeb.RootFolder;
            rootFolder.WelcomePage = curWeb.AllProperties[constPropertyForOldWelcomePage].ToString();
            rootFolder.Update();
            curWeb.Properties.Remove(constPropertyForOldCustomMaster);
            curWeb.Properties.Remove(constPropertyForOldWelcomePage);
            curWeb.Update();
            try
            {
                
                SPFile mundMasterPage = curWeb.GetFile(constMundMasterPagePath);
                mundMasterPage.Delete();

                SPFolder mundMasterPageFiles = curWeb.GetFolder(constmundMasterPageFilesPath);
                mundMasterPageFiles.Delete();
            }
            catch
            {
            }
           
        }

    }
}
